<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
	<div>
		<input id="sendMsg"/>
		<button onclick="sendMessage()">发送消息</button>
	</div>
	<div>
		<div id="receiveMsg"></div>
	</div>
<script>
	var websocket = null;
	// 判读浏览器是否支持webSocket
	if ("WebSocket" in window) {
		websocket = new WebSocket("ws://localhost:8098/webSocket2");
	}else{
		alert("浏览器不支持webSocket");
	}
	// 连接成功的回调函数
	websocket.onopen = function(){
		receiveMessageInnerHTML("webSocket连接成功");
	}
	// 连接错误的回调函数
	websocket.onerror = function(){
		receiveMessageInnerHTML("webSocket连接发生错误");
	}
	// 连接关闭的回调函数
	websocket.onclose = function(){
		receiveMessageInnerHTML("webSocket断开连接，请刷新重试！");
	}
	// 监听窗口的关闭
	window.onbeforeunload = function(){
		websocket.close();
	}
	// 显示消息到id=receiveMsg上
	function receiveMessageInnerHTML(innerHtml){
		document.getElementById("receiveMsg").innerHTML += innerHtml + "<br/>";
	}
	// 发送消息到服务器端
	function sendMessage(){
		var message = document.getElementById("sendMsg").value;
		websocket.send(message);
	}
	// 从服务器端 接受消息
	websocket.onmessage = function(event){
		receiveMessageInnerHTML("来自服务端的消息："+event.data);
	}
</script>
</body>
</html>